# -*- coding: utf-8 -*-
# $Id: problemwidget.py 27 2009-02-15 19:46:27Z gkuznets $

# Copyright 2008, 2009 Grigoriy Kuznetsov (gkuznets@yandex.ru)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from PyQt4 import QtGui
from PyQt4.QtGui import QWidget, QLabel, QListView, QListWidgetItem, QProgressBar
from checkup import Checkup
checkup = Checkup.instance()
from core.batch import Batch
from core.group import Group
from groupmodel import GroupModel
import mainwindow

class ProblemWidget(QWidget):
    """Виджет 'Группа проверки'"""
    
    def __init__(self, group):
        QWidget.__init__(self)
        self.group = group
        self.setAcceptDrops(True)
        # Создаем внутренность виджета
        layout = QtGui.QVBoxLayout()
        layout.setMargin(0)
        layout.setSpacing(2)
        #self.info_label = QLabel(u"Проверено 0 (0.00/мин.)")
        #layout.addWidget(self.info_label)
        self.progress_bars = [QProgressBar(), QProgressBar()]
        self.progress_bars[0].setStyleSheet("QProgressBar::chunk { background-color: #00FF00; }")
        self.progress_bars[1].setStyleSheet("QProgressBar::chunk { background-color: #0000FF; }")
        for p in self.progress_bars:
            p.setTextVisible(True) # Отображать или нет число процентов
            p.setVisible(False) # Не показываем до начала соответствующей проверки
            p.setValue(0)
            p.setMinimum(0)
            p.setMaximum(100)
            layout.addWidget(p)
        self.__group_model = GroupModel(self.group, self)
        self.__batches_view = QListView()
        self.__batches_view.setModel(self.__group_model)
        layout.addWidget(self.__batches_view)
        self.setLayout(layout)
        self.setMinimumSize(100, 100)
        
    def model(self):
        return self.__group_model
    
    def dragEnterEvent(self, QDragEnterEvent):
        print "dragEnterEvent"
    
    def dragLeaveEvent(self, QDragLeaveEvent):
        print "dragLeaveEvent"
  
    def dropEvent(self, QDropEvent):
        print "dropEvent"
    
    def update(self):
        for cnum in [0, 1]:
            if checkup.checkup() >= cnum:
                self.progress_bars[cnum].setVisible(True)
            self.progress_bars[cnum].setValue(self.group.progress(cnum))
        
        self.markOldBatches()
        
        QWidget.update(self)
        
    def markOldBatches(self):
        pass


class GroupsArea(QtGui.QScrollArea):
    def __init__(self, problems_count):
        QtGui.QScrollArea.__init__(self)
        layout_container = QWidget()
        hlayout = QtGui.QHBoxLayout()
        self.__wlist = []
        for i in range(problems_count):
            gw = ProblemWidget(Group(i))
            hlayout.addWidget(gw)
            self.__wlist.append(gw)
        layout_container.setLayout(hlayout)
        self.setWidget(layout_container)
        self.setWidgetResizable(True)
    
    def update(self):
        QtGui.QScrollArea.update(self)
        for gw in self.__wlist:
            gw.update()
    
    def problemWidgets(self):
        return self.__wlist